package com.runda.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.bbjob.mapper.ExamineeScoresMapper;
import com.bbjob.mapper.SysOrganizationMapper;
import com.bbjob.mapper.SysOrganizationStuMapper;
import com.bbjob.model.ExamineeScores;
import com.bbjob.model.ExamineeScoresDetail;
import com.bbjob.model.SysOrganization;
import com.bbjob.model.SysOrganizationStu;
import com.bbjob.util.service.impl.BaseService;
import com.github.pagehelper.PageHelper;
import com.runda.query.QueryScores;
import com.runda.service.IExamineeScoresService;

@Service
public class ExamineeScoresService extends BaseService<ExamineeScores> implements IExamineeScoresService {
	
	@Resource
	private ExamineeScoresMapper examineeScoresMapper;
	@Resource
	private SysOrganizationMapper sysOrganizationMapper;
	@Resource
	private SysOrganizationStuMapper sysOrganizationStuMapper;

	@Override
	public List<ExamineeScores> selectByParams(QueryScores scores) {
		
		if (scores.getUniquecode() != null && !"".equals(scores.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", scores.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				scores.setOrganSchool(organs.get(0).getId());
			}
		}

		PageHelper.startPage(scores.getPageNum(), scores.getPageSize());
		
		Example example = new Example(ExamineeScores.class);
		Criteria c = example.createCriteria();

		if (scores.getExamInfoId() != null) {
			c.andEqualTo("examInfoId", scores.getExamInfoId());
		}

		if (scores.getqId() != null) {
			c.andEqualTo("qId", scores.getqId());
		}

		if (scores.getsNo() != null) {
			c.andEqualTo("sNo", scores.getsNo());
		}

		if (scores.getOrganSchool() != null) {
			c.andEqualTo("organSchool", scores.getOrganSchool());
		}

		if (scores.getOrganDepart() != null) {
			c.andEqualTo("organDepart", scores.getOrganSchool());
		}

		if (scores.getOrganGrade() != null) {
			c.andEqualTo("organGrade", scores.getOrganSchool());
		}

		if (scores.getOrganClass() != null) {
			c.andEqualTo("organClass", scores.getOrganSchool());
		}
		
		List<ExamineeScores> list = examineeScoresMapper.selectByExample(example);

		Example stuExample = new Example(SysOrganizationStu.class);
		for (ExamineeScores item : list) {
			stuExample.clear();
			stuExample.createCriteria().andEqualTo("organSchool", scores.getOrganSchool()).andEqualTo("sno", item.getsNo());
			
			List<SysOrganizationStu> stus = sysOrganizationStuMapper.selectByExample(stuExample);
			
			if (stus.size() > 0) {
				item.setsName(stus.get(0).getSname());
				item.setDepartName(stus.get(0).getDepartName());
				item.setGradeName(stus.get(0).getGradeName());
				item.setClassName(stus.get(0).getClassName());
			}
		}
		
		return list;
	}

	@Override
	public void saveByScores(ExamineeScores scores) {
		
		if (scores.getUniquecode() != null && !"".equals(scores.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", scores.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				scores.setOrganSchool(organs.get(0).getId());
			}
		}
		
		Example stuExample = new Example(SysOrganizationStu.class);
		stuExample.createCriteria().andEqualTo("organSchool", scores.getOrganSchool()).andEqualTo("sno", scores.getsNo());
		List<SysOrganizationStu> list = sysOrganizationStuMapper.selectByExample(stuExample);
		
		if (list.size() > 0) {
			SysOrganizationStu stu = list.get(0);
			scores.setOrganDepart(stu.getOrganDepart());
			scores.setOrganGrade(stu.getOrganGrade());
			scores.setOrganClass(stu.getOrganClass());
		}
		
		Example scoreExample = new Example(ExamineeScores.class);
		scoreExample.createCriteria().andEqualTo("examInfoId", scores.getExamInfoId()).andEqualTo("organSchool", scores.getOrganSchool()).andEqualTo("qId", scores.getqId()).andEqualTo("sNo", scores.getsNo());
		List<ExamineeScores> items = examineeScoresMapper.selectByExample(scoreExample);
		if (items.size() > 0) {
			scores.setId(items.get(0).getId());
			scores.setUpdateTime(new Date());
			examineeScoresMapper.updateByPrimaryKeySelective(scores);
		} else {
			scores.setCreateTime(new Date());
			scores.setUpdateTime(new Date());
			examineeScoresMapper.insert(scores);
		}
	}
	
	@Override
	public List<ExamineeScores> selectAllByParams(QueryScores scores) {
		
		if (scores.getUniquecode() != null && !"".equals(scores.getUniquecode())) {
			Example organExample = new Example(SysOrganization.class);
			organExample.createCriteria().andEqualTo("uniqueCode", scores.getUniquecode());
			List<SysOrganization> organs = sysOrganizationMapper.selectByExample(organExample);
			
			if (organs.size() > 0) {
				scores.setOrganSchool(organs.get(0).getId());
			}
		}
		
		return examineeScoresMapper.selectAllByParams(scores.getOrganSchool(), scores.getOrganDepart(), scores.getOrganGrade(), scores.getOrganClass(), scores.getExamInfoId(), scores.getqId(), scores.getsNo());
	}
}
